package com.jali.crud.service;

import com.jali.crud.bean.Emp;
import com.jali.crud.bean.EmpExample;
import com.jali.crud.dao.EmpMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author jali
 * @create 2018-12-04 17:30
 */
@Service
public class EmpService {

    @Autowired
    EmpMapper empMapper;


    public List<Emp> getEmps() {
        return empMapper.selectByExampleWithDept(null);
    }

    public void save(Emp emp) {
        empMapper.insertSelective(emp);
    }

    /**
     * 校验用户名是否可用
     * @param empName
     * @return true：代表可用，没有重复
     */
    public boolean checkUser(String empName) {
        EmpExample empExample = new EmpExample();
        EmpExample.Criteria criteria = empExample.createCriteria();
        criteria.andempNameEqualTo(empName);
        long count = empMapper.countByExample(empExample);
        return count==0;
    }

    public Emp getEmp(Integer id) {
        return empMapper.selectByPrimaryKey(id);
    }

    public void updatEmp(Emp emp) {
        empMapper.updateByPrimaryKeySelective(emp);
    }

    public void deleteEmp(Integer id) {
        empMapper.deleteByPrimaryKey(id);
    }

    public void deleteBatch(List<Integer> idsList) {
        EmpExample empExample = new EmpExample();
        EmpExample.Criteria criteria = empExample.createCriteria();
        criteria.andIdIn(idsList);
        empMapper.deleteByExample(empExample);
    }
}
